﻿using SHKF.B2C.Model.Dto;
using SHKF.B2C.Model.Dto.Center;
using SHKF.B2C.Model.Entity.Center;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WQDSkin.Forms.Component;
using WQDSkin.Forms.Controls;  

namespace SHKF.B2C.Mannage.AdminUI.View.CenterInfo.User
{
    /// <summary>
    /// frmUser.xaml 的交互逻辑
    /// </summary>
    public partial class frmUser : BasePage
    {
        public frmUser()
        {
            InitializeComponent();
        }

        public override void CallBack(object model)
        {
            base.CallBack(model);

            LoadUser();
        }


        private void BasePage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadCompany();
        }

        private void btnSearchCompany_Click(object sender, RoutedEventArgs e)
        {
            LoadCompany();
        }

        private async void LoadCompany()
        {
            string CompanyName = this.txtCompany.Text;
            CompanyListDto dto = await GL.HttpClientManage.QueryCompanyList(CompanyName);

            this.tvCompany.Items.Clear();

            if (dto.error == null)
            {
                foreach (CompanyEntity company in dto.data)
                {
                    WQDTreeViewItem tviCompany = new WQDTreeViewItem();
                    tviCompany.DataContext = company;
                    tviCompany.Header = company.CompanyName;
                    this.tvCompany.Items.Add(tviCompany);
                }
            }
            else
            {
                MessageBox.Show(dto.error.msg);
            }
        }

        private async void tvCompany_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.tvCompany.SelectData is CompanyEntity)
            {
                WQDTreeViewItem tviSelected = (WQDTreeViewItem)this.tvCompany.SelectedItem;
                if (tviSelected.Items.Count == 0)
                {
                    CompanyEntity company = (CompanyEntity)this.tvCompany.SelectData;
                    ShopListDto list = await GL.HttpClientManage.ShopQuery((int)company.SN, "");
                    tviSelected.DisplayMemberPath = "ShopName";
                    tviSelected.ItemsSource = list.data;
                }
            }
        }
         
        private void btUserAdd_Click(object sender, RoutedEventArgs e)
        {
            int CompanySN = 0;
            int ShopSN = 0;

            if (this.tvCompany.SelectedItem is TreeViewItem)
            {
                CompanySN = (int)((CompanyEntity)this.tvCompany.SelectData).SN;
                ShopSN = -1;
            }
            if (this.tvCompany.SelectedItem is ShopEntity)
            {
                ShopEntity shop = (ShopEntity)this.tvCompany.SelectedItem;
                CompanySN = shop.CompanySN;
                ShopSN = (int)shop.ShopSN;
            }

            UserEntity shopEntity = new UserEntity();
            shopEntity.CompanySN = CompanySN;
            shopEntity.ShopSN = ShopSN;

            base.Navigate("/View/CenterInfo/User/frmUserEdit", shopEntity);
        }

        private void btnUserEdit_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgv.SelectedItems.Count > 0)
            {
                UserEntity user = (UserEntity)this.dgv.SelectedValue;
                base.Contain.Navigate("/View/CenterInfo/User/frmUserEdit", user.Clone());
            }
        }

        private async void btnUserDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("请确认是否要删除", "删除提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                UserEntity user = (UserEntity)this.dgv.SelectedValue;
                DataDto<int> dto = await GL.HttpClientManage.DeleteUser(user.SN);
                if (dto.error == null)
                {
                    this.LoadUser();
                }
            }
        }
 
       
        private void btnSearchUser_Click(object sender, RoutedEventArgs e)
        {
            LoadUser();
        }

        private async void LoadUser()
        {
            int CompanySN = 0;
            int ShopSN = -1;

            if (this.tvCompany.SelectedItem is TreeViewItem)
            {
                CompanySN = (int)((CompanyEntity)this.tvCompany.SelectData).SN;
            }
            if (this.tvCompany.SelectedItem is ShopEntity)
            {
                ShopEntity shop = (ShopEntity)this.tvCompany.SelectedItem;
                CompanySN = shop.CompanySN;
                ShopSN = (int)shop.ShopSN;
            }
            DataDto<List<UserEntity>> list = await GL.HttpClientManage.QueryUserList(CompanySN, ShopSN, this.txtUser.Text);
            this.dgv.ItemsSource = list.data;
        }
    }
}

